package cn.shutdown.controller;

import cn.shutdown.po.Category;
import cn.shutdown.po.Post;
import cn.shutdown.po.ResourceEntity;
import cn.shutdown.po.User;
import cn.shutdown.service.ICategoryService;
import cn.shutdown.service.IPostService;
import cn.shutdown.service.IResourceService;
import cn.shutdown.service.IUserService;
import cn.shutdown.util.MessageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by wejack on 2016/9/24.
 */
@Controller
public class LoginController {
    /**
     * 用户服务
     */
    @Autowired
    private IUserService userService;

    /**
     * 栏目服务
     */
    @Autowired
    private ICategoryService categoryService;

    /**
     * 帖子服务
     */
    @Autowired
    private IPostService postService;

    /**
     * 资源服务
     */
    @Autowired
    private IResourceService resourceService;

    @RequestMapping(value = "/login/index.do")
    public ModelAndView indexPage(HttpServletRequest request) {
        ModelAndView mav = new ModelAndView();
        List<Category> list = categoryService.findAll();
        request.setAttribute("categoryList", list);
        Map<Integer, List<Map<String, Object>>> map = new HashMap<Integer, List<Map<String, Object>>>();
        List<Map<String, Object>> postMap;
        for (int i = 0; i < list.size(); i++) {
            postMap = postService.findByCategoryId(list.get(i).getId());
            map.put(list.get(i).getId(), postMap);
        }
        request.setAttribute("postMap", map);
        return mav;
    }

    /**
     * 登录页面
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/login/loginPage.do")
    public ModelAndView loginPage(HttpServletRequest request) {
        ModelAndView mav = new ModelAndView();
        MessageUtil.buildModelAndViewMessage(request);
        return mav;
    }

    /**
     * 登录
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/login/login.do", method = RequestMethod.POST)
    public ModelAndView login(HttpServletRequest request) {
        ModelAndView mav = new ModelAndView();
        String userName = request.getParameter("userName");
        String password = request.getParameter("password");
        User user = userService.matchUser(userName, password);
        if (user != null) {
            mav.setViewName("redirect:/login/index.do");
            request.getSession().setAttribute("user", user);
            if ("admin".equals(user.getUserName())) {
                mav.setViewName("redirect:/login/adminIndex.do");
            }
        } else {
            mav.setViewName("redirect:/login/loginPage.do");
            MessageUtil.setModelAndViewMessage(mav, MessageUtil.TYPE_WARN, "用户名或密码错误");
        }
        return mav;
    }

    /**
     * 注册页面
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/login/registPage.do")
    public ModelAndView registPage(HttpServletRequest request) {
        ModelAndView mav = new ModelAndView();
        return mav;
    }

    /**
     * 注册操作
     *
     * @param user
     * @return
     */
    @RequestMapping(value = "/login/regist.do", method = RequestMethod.POST)
    public ModelAndView regist(@ModelAttribute User user) {
        ModelAndView mav = new ModelAndView();
        userService.registUser(user);
        mav.setViewName("redirect:/login/index.do");
        return mav;
    }

    /**
     * 登录操作
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/login/logout.do")
    public ModelAndView logout(HttpServletRequest request) {
        ModelAndView mav = new ModelAndView();
        request.getSession().removeAttribute("user");
        mav.setViewName("redirect:/login/index.do");
        return mav;
    }

    /**
     * 管理员登录
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/login/adminIndex.do")
    public ModelAndView adminIndex(HttpServletRequest request) {
        ModelAndView mav = new ModelAndView();
        mav.setViewName("/login/adminIndex");
        return mav;
    }

    /**
     * 查看帖子
     *
     * @param id
     * @param request
     * @return
     */
    @RequestMapping(value = "/post/{id}")
    public ModelAndView toPost(@PathVariable(value = "id") int id, HttpServletRequest request) {
        ModelAndView mav = new ModelAndView();
        Map<String, Object> post = postService.findById(id);
        request.setAttribute("post", post);
        List<Map<String, Object>> subPost = postService.findSubPost(id);
        request.setAttribute("subPost", subPost);
        mav.setViewName("/post/post");
        return mav;
    }

    /**
     * 回复帖子
     *
     * @param id
     * @param content
     * @param request
     * @return
     */
    @RequestMapping(value = "/post/reply/{id}", method = RequestMethod.POST)
    public ModelAndView postReply(@PathVariable(value = "id") int id, @RequestParam(value = "content") String content, HttpServletRequest request) {
        ModelAndView mav = new ModelAndView();
        Map<String, Object> post = postService.findById(id);
        Integer maxLevel = postService.getMaxPostLevel(id);
        Post replyPost = new Post();
        replyPost.setCategoryId((Integer) post.get("categoryId"));
        replyPost.setPid(id);
        replyPost.setContent(content);
        User user = (User) request.getSession().getAttribute("user");
        replyPost.setUserId(user.getId());
        replyPost.setCreateDate(new Date());
        replyPost.setLevel(maxLevel + 1);
        postService.save(replyPost);
        mav.setViewName("redirect:/post/" + id);
        return mav;
    }

    /**
     * 查看栏目下的帖子
     *
     * @param request
     * @param categoryId
     * @return
     */
    @RequestMapping(value = "/post/list/{categoryId}")
    public ModelAndView listPage(HttpServletRequest request, @PathVariable(value = "categoryId") int categoryId) {
        ModelAndView mav = new ModelAndView();
        List<Map<String, Object>> postList = postService.getPostListByCategoryId(categoryId);
        request.setAttribute("postList", postList);
        mav.setViewName("/post/list");
        Category category = categoryService.findById(categoryId);
        request.setAttribute("category", category);
        return mav;
    }

    /**
     * 添加帖子页面
     *
     * @param request
     * @param categoryId
     * @return
     */
    @RequestMapping(value = "/post/add/{categoryId}")
    public ModelAndView addPage(HttpServletRequest request, @PathVariable(value = "categoryId") int categoryId) {
        ModelAndView mav = new ModelAndView();
        User user = (User) request.getSession().getAttribute("user");
        if (user == null) {
            mav.setViewName("/login/loginPage");
            return mav;
        }
        mav.setViewName("/post/add");
        Category category = categoryService.findById(categoryId);
        request.setAttribute("category", category);
        return mav;
    }

    /**
     * 添加帖子保存
     *
     * @param post
     * @return
     */
    @RequestMapping(value = "/post/add.do", method = RequestMethod.POST)
    public ModelAndView addPost(@ModelAttribute Post post) {
        ModelAndView mav = new ModelAndView();
        post.setCreateDate(new Date());
        postService.save(post);
        mav.setViewName("redirect:/post/" + post.getId());
        return mav;
    }

    /**
     * 栏目列表
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/category/list.do")
    public ModelAndView categoryList(HttpServletRequest request) {
        ModelAndView mav = new ModelAndView();
        List<Category> list = categoryService.findAll();
        request.setAttribute("categoryList", list);
        return mav;
    }

    /**
     * 添加栏目页面
     *
     * @return
     */
    @RequestMapping(value = "/category/add.do")
    public ModelAndView addCategory() {
        ModelAndView mav = new ModelAndView();
        return mav;
    }

    /**
     * 添加栏目保存
     *
     * @param category
     * @return
     */
    @RequestMapping(value = "/category/add.do", method = RequestMethod.POST)
    public ModelAndView saveCategory(@ModelAttribute Category category) {
        ModelAndView mav = new ModelAndView();
        categoryService.save(category);
        return mav;
    }

    /**
     * 资源列表
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/resource/list.do", method = RequestMethod.GET)
    public ModelAndView resourceList(HttpServletRequest request) {
        ModelAndView mav = new ModelAndView();
        List<ResourceEntity> resourceEntities = resourceService.findAll();
        request.setAttribute("resourceList", resourceEntities);
        return mav;
    }

    @RequestMapping(value = "/resource/add.do")
    public ModelAndView addResourcePage() {
        ModelAndView mav = new ModelAndView();
        return mav;
    }

    @RequestMapping(value = "/resource/add.do", method = RequestMethod.POST)
    public ModelAndView addResource(@ModelAttribute ResourceEntity resourceEntity) {
        ModelAndView mav = new ModelAndView();
        resourceService.save(resourceEntity);
        mav.setViewName("redirect:/resource/" + resourceEntity.getId());
        return mav;
    }

    @RequestMapping(value = "/resource/{id}")
    public ModelAndView toResource(HttpServletRequest request, @PathVariable(value = "id") int resourceId) {
        ModelAndView mav = new ModelAndView();
        ResourceEntity resource = resourceService.findById(resourceId);
        request.setAttribute("resource", resource);
        mav.setViewName("/resource/resource");
        return mav;
    }
}
